home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC / src / dlgprnt.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  10.7 KB  |  398 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12. #include <dlgs.h>       // for standard control IDs for commdlg
  13.  
  14. #ifdef AFX_AUX_SEG
  15. #pragma code_seg(AFX_AUX_SEG)
  16. #endif
  17.  
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. #define new DEBUG_NEW
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // Page Setup dialog
  27.  
  28. CPageSetupDialog::CPageSetupDialog(DWORD dwFlags, CWnd* pParentWnd) :
  29.     CCommonDialog(pParentWnd)
  30. {
  31.     memset(&m_psd, 0, sizeof(m_psd));
  32.  
  33.     m_psd.lStructSize = sizeof(m_psd);
  34.     m_psd.Flags = (dwFlags | PSD_ENABLEPAGESETUPHOOK | PSD_ENABLEPAGEPAINTHOOK);
  35.     if (!afxData.bWin4 && AfxHelpEnabled())
  36.         m_psd.Flags |= PSD_SHOWHELP;
  37.     m_psd.lpfnPageSetupHook = (COMMDLGPROC)_AfxCommDlgProc;
  38.     m_psd.lpfnPagePaintHook = (COMMDLGPROC)CPageSetupDialog::PaintHookProc;
  39. }
  40.  
  41. int CPageSetupDialog::DoModal()
  42. {
  43.     ASSERT_VALID(this);
  44.     ASSERT(m_psd.Flags & PSD_ENABLEPAGESETUPHOOK);
  45.     ASSERT(m_psd.Flags & PSD_ENABLEPAGEPAINTHOOK);
  46.     ASSERT(m_psd.lpfnPageSetupHook != NULL); // can still be a user hook
  47.     ASSERT(m_psd.lpfnPagePaintHook != NULL); // can still be a user hook
  48.  
  49.     m_psd.hwndOwner = PreModal();
  50.     int nResult = ::PageSetupDlg(&m_psd);
  51.     PostModal();
  52.     return nResult ? nResult : IDCANCEL;
  53. }
  54.  
  55. ////////////////////////////////////////////////////////////////////////////
  56. // CPageSetupDialog attributes
  57.  
  58. LPDEVMODE CPageSetupDialog::GetDevMode() const
  59. {
  60.     if (m_psd.hDevMode == NULL)
  61.         return NULL;
  62.  
  63.     return (LPDEVMODE)::GlobalLock(m_psd.hDevMode);
  64. }
  65.  
  66. CString CPageSetupDialog::GetDriverName() const
  67. {
  68.     if (m_psd.hDevNames == NULL)
  69.         return afxEmptyString;
  70.  
  71.     LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(m_psd.hDevNames);
  72.     return (LPCTSTR)lpDev + lpDev->wDriverOffset;
  73. }
  74.  
  75. CString CPageSetupDialog::GetDeviceName() const
  76. {
  77.     if (m_psd.hDevNames == NULL)
  78.         return afxEmptyString;
  79.  
  80.     LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(m_psd.hDevNames);
  81.     return (LPCTSTR)lpDev + lpDev->wDeviceOffset;
  82. }
  83.  
  84. CString CPageSetupDialog::GetPortName() const
  85. {
  86.     if (m_psd.hDevNames == NULL)
  87.         return afxEmptyString;
  88.  
  89.     LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(m_psd.hDevNames);
  90.     return (LPCTSTR)lpDev + lpDev->wOutputOffset;
  91. }
  92.  
  93. // Create an HDC from the devnames and devmode.
  94. HDC CPageSetupDialog::CreatePrinterDC()
  95. {
  96.     ASSERT_VALID(this);
  97.     return AfxCreateDC(m_psd.hDevNames, m_psd.hDevMode);
  98. }
  99.  
  100. void CPageSetupDialog::GetMargins(LPRECT lpRectMargins, LPRECT lpRectMinMargins) const
  101. {
  102.     if (lpRectMargins != NULL)
  103.         memcpy(lpRectMargins, &m_psd.rtMargin, sizeof(RECT));
  104.     if (lpRectMinMargins != NULL)
  105.         memcpy(lpRectMinMargins, &m_psd.rtMinMargin, sizeof(RECT));
  106. }
  107.  
  108. ////////////////////////////////////////////////////////////////////////////
  109. // CPageSetupDialog diagnostics
  110.  
  111. #ifdef _DEBUG
  112. void CPageSetupDialog::Dump(CDumpContext& dc) const
  113. {
  114.     CDialog::Dump(dc);
  115.  
  116.     dc << "m_psd.hwndOwner = " << (UINT)m_psd.hwndOwner;
  117.     dc << "\nm_psd.Flags = " << (LPVOID)m_psd.Flags;
  118.  
  119.     dc << "\nm_psd.ptPaperSize = " << m_psd.ptPaperSize;
  120.     dc << "\nm_psd.rtMinMargin = " << m_psd.rtMinMargin;
  121.     dc << "\nm_psd.rtMinMargin = " << m_psd.rtMinMargin;
  122.  
  123.     if (m_psd.lpfnPageSetupHook == (COMMDLGPROC)_AfxCommDlgProc)
  124.         dc << "\nsetup hook function set to standard MFC hook function";
  125.     else
  126.         dc << "\nsetup hook function set to non-standard hook function";
  127.  
  128.     if (m_psd.lpfnPagePaintHook == (COMMDLGPROC)_AfxCommDlgProc)
  129.         dc << "\nprint hook function set to standard MFC hook function";
  130.     else
  131.         dc << "\nprint hook function set to non-standard hook function";
  132.  
  133.     dc << "\n";
  134. }
  135. #endif //_DEBUG
  136.  
  137. ////////////////////////////////////////////////////////////////////////////
  138. // CPageSetupDialog hook
  139.  
  140. UINT CPageSetupDialog::PreDrawPage(WORD /*wPaperType*/, WORD /*wFlags*/,
  141.     LPPAGESETUPDLG)
  142. {
  143.     return 0;
  144.     //return 1 to prevent any more drawing
  145. }
  146.  
  147. UINT CPageSetupDialog::OnDrawPage(CDC*, UINT /*nMessage*/, LPRECT)
  148. {
  149.     return 0; // do the default
  150. }
  151.  
  152. UINT CALLBACK CPageSetupDialog::PaintHookProc(HWND hWnd, UINT message, WPARAM wParam,
  153.     LPARAM lParam)
  154. {
  155.     if (hWnd == NULL)
  156.         return 0;
  157.     // Get our Window
  158.     // assume it is already wired up to a permanent one
  159.     // the hWnd is the HWND of a control in the page setup proc
  160.     CPageSetupDialog* pDlg = DYNAMIC_DOWNCAST(CPageSetupDialog,
  161.         CWnd::FromHandlePermanent(::GetParent(hWnd)));
  162.     if (pDlg == NULL)
  163.         return 0;
  164.     switch (message)
  165.     {
  166.     case WM_PSD_PAGESETUPDLG:
  167.         return pDlg->PreDrawPage(LOWORD(wParam), HIWORD(wParam),
  168.             (LPPAGESETUPDLG) lParam);
  169.         break;
  170.     case WM_PSD_FULLPAGERECT:
  171.     case WM_PSD_MINMARGINRECT:
  172.     case WM_PSD_MARGINRECT:
  173.     case WM_PSD_GREEKTEXTRECT:
  174.     case WM_PSD_ENVSTAMPRECT:
  175.     case WM_PSD_YAFULLPAGERECT:
  176.         return pDlg->OnDrawPage(CDC::FromHandle((HDC)wParam), message, (LPRECT)lParam);
  177.         break;
  178.     }
  179.     return 0;
  180. }
  181.  
  182. /////////////////////////////////////////////////////////////////////////////
  183. // Print/Print Setup dialog
  184.  
  185. BEGIN_MESSAGE_MAP(CPrintDialog, CCommonDialog)
  186.     //{{AFX_MSG_MAP(CPrintDialog)
  187.     ON_COMMAND(psh1, OnPrintSetup) // print setup button when print is displayed
  188.     //}}AFX_MSG_MAP
  189. END_MESSAGE_MAP()
  190.  
  191. CPrintDialog::CPrintDialog(BOOL bPrintSetupOnly,
  192.     DWORD dwFlags, CWnd* pParentWnd)
  193.     : m_pd(m_pdActual), CCommonDialog(pParentWnd)
  194. {
  195.     memset(&m_pdActual, 0, sizeof(m_pdActual));
  196.  
  197.     m_pd.lStructSize = sizeof(m_pdActual);
  198.     m_pd.Flags = (dwFlags | PD_ENABLEPRINTHOOK | PD_ENABLESETUPHOOK);
  199.     if (!afxData.bWin4 && AfxHelpEnabled())
  200.         m_pd.Flags |= PD_SHOWHELP;
  201.     m_pd.lpfnPrintHook = (COMMDLGPROC)_AfxCommDlgProc;
  202.     m_pd.lpfnSetupHook = (COMMDLGPROC)_AfxCommDlgProc;
  203.  
  204.     if (bPrintSetupOnly)
  205.     {
  206.         m_nIDHelp = AFX_IDD_PRINTSETUP;
  207.         m_pd.Flags |= PD_PRINTSETUP;
  208.     }
  209.     else
  210.     {
  211.         m_nIDHelp = AFX_IDD_PRINT;
  212.         m_pd.Flags |= PD_RETURNDC;
  213.     }
  214.  
  215.     m_pd.Flags &= ~PD_RETURNIC; // do not support information context
  216. }
  217.  
  218. // Helper ctor for AttachOnSetup
  219. CPrintDialog::CPrintDialog(PRINTDLG& pdInit)
  220.     : m_pd(pdInit), CCommonDialog(NULL)
  221. {
  222. }
  223.  
  224. // Function to keep m_pd in sync after user invokes Setup from
  225. // the print dialog (via the Setup button)
  226. // If you decide to handle any messages/notifications and wish to
  227. // handle them differently between Print/PrintSetup then override
  228. // this function and create an object of a derived class
  229. CPrintDialog* CPrintDialog::AttachOnSetup()
  230. {
  231.     ASSERT_VALID(this);
  232.  
  233.     CPrintDialog* pDlgSetup;
  234.  
  235.     pDlgSetup = new CPrintDialog(m_pd);
  236.     pDlgSetup->m_hWnd = NULL;
  237.     pDlgSetup->m_pParentWnd = m_pParentWnd;
  238.     pDlgSetup->m_nIDHelp = AFX_IDD_PRINTSETUP;
  239.     return pDlgSetup;
  240. }
  241.  
  242. void CPrintDialog::OnPrintSetup()
  243. {
  244.     ASSERT_VALID(this);
  245.  
  246.     CPrintDialog* pDlgSetup = AttachOnSetup();
  247.     ASSERT(pDlgSetup != NULL);
  248.  
  249.     AfxHookWindowCreate(pDlgSetup);
  250.     Default();
  251.     AfxUnhookWindowCreate();
  252.  
  253.     delete pDlgSetup;
  254. }
  255.  
  256. int CPrintDialog::DoModal()
  257. {
  258.     ASSERT_VALID(this);
  259.     ASSERT(m_pd.Flags & PD_ENABLEPRINTHOOK);
  260.     ASSERT(m_pd.Flags & PD_ENABLESETUPHOOK);
  261.     ASSERT(m_pd.lpfnPrintHook != NULL); // can still be a user hook
  262.     ASSERT(m_pd.lpfnSetupHook != NULL); // can still be a user hook
  263.  
  264.     m_pd.hwndOwner = PreModal();
  265.     int nResult = ::PrintDlg(&m_pd);
  266.     PostModal();
  267.     return nResult ? nResult : IDCANCEL;
  268. }
  269.  
  270. // Create an HDC without calling DoModal.
  271. HDC CPrintDialog::CreatePrinterDC()
  272. {
  273.     ASSERT_VALID(this);
  274.     m_pd.hDC = AfxCreateDC(m_pd.hDevNames, m_pd.hDevMode);
  275.     return m_pd.hDC;
  276. }
  277.  
  278. int CPrintDialog::GetCopies() const
  279. {
  280.     ASSERT_VALID(this);
  281.  
  282.     if (m_pd.Flags & PD_USEDEVMODECOPIES)
  283.         return GetDevMode()->dmCopies;
  284.     else
  285.         return m_pd.nCopies;
  286. }
  287.  
  288. LPDEVMODE CPrintDialog::GetDevMode() const
  289. {
  290.     if (m_pd.hDevMode == NULL)
  291.         return NULL;
  292.  
  293.     return (LPDEVMODE)::GlobalLock(m_pd.hDevMode);
  294. }
  295.  
  296. CString CPrintDialog::GetDriverName() const
  297. {
  298.     if (m_pd.hDevNames == NULL)
  299.         return afxEmptyString;
  300.  
  301.     LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(m_pd.hDevNames);
  302.     return (LPCTSTR)lpDev + lpDev->wDriverOffset;
  303. }
  304.  
  305. CString CPrintDialog::GetDeviceName() const
  306. {
  307.     if (m_pd.hDevNames == NULL)
  308.         return afxEmptyString;
  309.  
  310.     LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(m_pd.hDevNames);
  311.     return (LPCTSTR)lpDev + lpDev->wDeviceOffset;
  312. }
  313.  
  314. CString CPrintDialog::GetPortName() const
  315. {
  316.     if (m_pd.hDevNames == NULL)
  317.         return afxEmptyString;
  318.  
  319.     LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(m_pd.hDevNames);
  320.     return (LPCTSTR)lpDev + lpDev->wOutputOffset;
  321. }
  322.  
  323. // this function must not be in afxdlgs.inl because of DLL delay loading
  324. BOOL CPrintDialog::GetDefaults()
  325. {
  326.     m_pd.Flags |= PD_RETURNDEFAULT;
  327.     return ::PrintDlg(&m_pd);
  328. }
  329.  
  330. ////////////////////////////////////////////////////////////////////////////
  331. // CPrintDialog diagnostics
  332.  
  333. #ifdef _DEBUG
  334. void CPrintDialog::Dump(CDumpContext& dc) const
  335. {
  336.     CDialog::Dump(dc);
  337.  
  338.     dc << "m_pd.hwndOwner = " << (UINT)m_pd.hwndOwner;
  339.  
  340.     if (m_pd.hDC != NULL)
  341.         dc << "\nm_pd.hDC = " << CDC::FromHandle(m_pd.hDC);
  342.  
  343.     dc << "\nm_pd.Flags = " << (LPVOID)m_pd.Flags;
  344.     dc << "\nm_pd.nFromPage = " << m_pd.nFromPage;
  345.     dc << "\nm_pd.nToPage = " << m_pd.nToPage;
  346.     dc << "\nm_pd.nMinPage = " << m_pd.nMinPage;
  347.     dc << "\nm_pd.nMaxPage = " << m_pd.nMaxPage;
  348.     dc << "\nm_pd.nCopies = " << m_pd.nCopies;
  349.  
  350.     if (m_pd.lpfnSetupHook == (COMMDLGPROC)_AfxCommDlgProc)
  351.         dc << "\nsetup hook function set to standard MFC hook function";
  352.     else
  353.         dc << "\nsetup hook function set to non-standard hook function";
  354.  
  355.     if (m_pd.lpfnPrintHook == (COMMDLGPROC)_AfxCommDlgProc)
  356.         dc << "\nprint hook function set to standard MFC hook function";
  357.     else
  358.         dc << "\nprint hook function set to non-standard hook function";
  359.  
  360.     dc << "\n";
  361. }
  362. #endif //_DEBUG
  363.  
  364. ////////////////////////////////////////////////////////////////////////////
  365. // AfxCreateDC
  366.  
  367. HDC AFXAPI AfxCreateDC(HGLOBAL hDevNames, HGLOBAL hDevMode)
  368. {
  369.     if (hDevNames == NULL)
  370.         return NULL;
  371.  
  372.     LPDEVNAMES lpDevNames = (LPDEVNAMES)::GlobalLock(hDevNames);
  373.     LPDEVMODE  lpDevMode = (hDevMode != NULL) ?
  374.                         (LPDEVMODE)::GlobalLock(hDevMode) : NULL;
  375.  
  376.     if (lpDevNames == NULL)
  377.         return NULL;
  378.  
  379.     HDC hDC = ::CreateDC((LPCTSTR)lpDevNames + lpDevNames->wDriverOffset,
  380.                       (LPCTSTR)lpDevNames + lpDevNames->wDeviceOffset,
  381.                       (LPCTSTR)lpDevNames + lpDevNames->wOutputOffset,
  382.                       lpDevMode);
  383.  
  384.     ::GlobalUnlock(hDevNames);
  385.     if (hDevMode != NULL)
  386.         ::GlobalUnlock(hDevMode);
  387.     return hDC;
  388. }
  389.  
  390. #ifdef AFX_INIT_SEG
  391. #pragma code_seg(AFX_INIT_SEG)
  392. #endif
  393.  
  394. IMPLEMENT_DYNAMIC(CPrintDialog, CDialog)
  395. IMPLEMENT_DYNAMIC(CPageSetupDialog, CDialog)
  396.  
  397. ////////////////////////////////////////////////////////////////////////////
  398.